package com.sunday.pinxinghui.model.products;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseLazyFragment;
import com.sunday.pinxinghui.entity.Brand;
import com.sunday.pinxinghui.widgets.SelectBrandWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author by Damon,  on 2017/12/1.
 */

public class ProListFragment extends BaseLazyFragment {


    @Bind(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.btn_select_brand)
    TextView btnSelectBrand;

    private int catId;


    public static ProListFragment newInstance(int catId) {
        ProListFragment fragment = new ProListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("catId", catId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            catId = getArguments().getInt("catId");
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_pro_list, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        if (!isPrepared || !isVisible) {
            return;
        }

        isPrepared = false;
//        memberId= BaseApplication.getInstance().getMember().getId();
//        ptrFrame.autoRefresh();
        try {
            initFragment();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Fragment[] fragments;
    private List<String> titles = new ArrayList<>();
    PagerAdapter adapter;

    private void initFragment() {
        titles.clear();
        titles.add("综合排序");
        titles.add("销量");
        //titles.add("品牌");
        titles.add("价格");
        adapter = new PagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(titles.size());
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {

            }

            @Override
            public void onTabReselect(int position) {
                if (position == 2) {
                    if (((ProductClassifyFragment) (fragments[2])).getSort() == 3) {
                        ((ProductClassifyFragment) (fragments[2])).setSort(4);
                    } else {
                        ((ProductClassifyFragment) (fragments[2])).setSort(3);
                    }
                    ((ProductClassifyFragment) (fragments[2])).refreshData();
                }
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private Integer brandId;

    public class PagerAdapter extends FragmentPagerAdapter {


        public PagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position] == null) {
                fragments[position] = ProductClassifyFragment.newInstance(position + 1, catId);
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // return super.instantiateItem(container, position);
            ProductClassifyFragment fragment = (ProductClassifyFragment) super.instantiateItem(container, position);
            //Tab tab = tabs.get(position);
            fragment.setBrandId(brandId);
            return fragment;
        }

        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }


    }


    private SelectBrandWindow selectBrandWindow;

    @OnClick(R.id.btn_select_brand)
    void selectBrand() {
        if (selectBrandWindow == null) {
            selectBrandWindow = new SelectBrandWindow(mContext, catId);
        }
        selectBrandWindow.showDropDown(btnSelectBrand);
        selectBrandWindow.setClick(new SelectBrandWindow.Click() {
            @Override
            public void click(Brand brand) {
                btnSelectBrand.setText(brand.getName());
                   /* for (int i=0;i<titles.size();i++){
                        ((ProductClassifyFragment) (fragments[i])).setBrandId(brand.getId());
                        if (fragments[i].getUserVisibleHint()){
                            ((ProductClassifyFragment) (fragments[i])).refreshData();
                        }
                    }*/
                brandId = brand.getId();
                adapter.notifyDataSetChanged();

            }
        });

    }

}
